thin.imports('/public/configure/conf.project.js');
thin.imports('/public/plug/laytpl/laytpl.js');
thin.imports('/public/webfe/resource/pro-delete.js');
thin.imports('/public/webfe/resource/pay.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	layer.load(0, {
		shade: [0.08, '#000']
	});

	// 加載一鍵融資列表
	factory.request('_project_cxlist');

	$('#project-list-view').on('click', '#submit-keyPush', isPerfectProject).on('click', '#btn-cph', cphistorylists);

	$(document).on('click', '.preview', function() {
		var uri = $(this).attr('data-uri');
		tool.__cookie.set('pdf_uri', uri);
		//window.location.href = '/pdf.html';
		window.open("/pdf.html");
	});

	// 绑定删除项目按钮
	$(document).on('click', '[layout-mark=pro-del]', thin.submitDeleteProject);

	// 绑定缴纳诚信保证金按钮
	$(document).on('click', '[layout-mark=topay]', thin.submitScanCodePay);

	// 监听协议按钮
	$(document).on('change', '#weuiAgrees', function() {
		var ischecked = $(this).prop('checked'),
			_p = $(this).parent().parent();
		if(ischecked) {
			_p.find('#submit-keyPush').removeClass('disabled');
		} else {
			_p.find('#submit-keyPush').addClass('disabled');
		}
	});

	// 删除评估报告
	$(document).on('click', '#btn-delpdf', function() {
		var _id = $(this).attr('data-id');
		layer.confirm('确定删除评估报告吗', {
			btn: ['删除', '取消'] //按钮
		}, function(index) {
			//点击确认后的回调函数
			layer.load(0, {
				shade: [0.08, '#000']
			});
			__getdata_deletecp['id'] = _id;
			factory.request('_project_deletecp', _id);
			layer.close(index);
		}, function() {});
	});

});

var __getdata_prokeyPush = {},
	chpdata = {},
	__getdata_procph = {},
	__getdata_deletecp = {};

// 扫码支付回调
function submitScanCodePayCallback() {
	window.location.reload();
}

// 删除评估报告回调
function __complete_deletecp(res, unkn) {
	layer.closeAll('loading');
	if (res.status == 204) {
		layer.msg('删除成功', {
			icon: 1,
			time: 1200
		}, function() {
			$('div[data-id=' + unkn + ']').remove();
			chpdata = {};
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 一鍵融資列表回調
function __complete_procxlist(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.length > 0) {
			var template = $('#project-list-template').html();
			laytpl(template).render(res.data, function(str) {
				$('#project-list-view').html(str);
			});
			//$('#project-list-view').parent().removeClass('nothing-bg');
			$('.nothing-box').hide();
		} else {
			// 暫無内容
			/*$('#project-list-view').html('').show();
			$('#project-list-view').parent().addClass('nothing-bg');*/
			$('#project-list-view').html('');
			$('.nothing-box').show();
		}
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

var __getdata_checkcomplete = {};

// 检测是否完善项目
function isPerfectProject() {
	if($(this).hasClass('disabled')) return;
	var _id = $(this).attr('data-id');

	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_checkcomplete['project_id'] = _id;
	factory.request('_project_checkcomplete', $(this));
}

// 检测是否完善项目回调
function __complete_checkcomplete(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.ret == true) {
			submitKeyPush(unkn);
		} else {
			var ele = '';
			ele += '<div style="line-height: 32px;">';
			ele += '以下信息尚未完善：';
			for(var i in res.data.msg) {
				ele += '<br />';
				ele += '<font class="m-r-1x">·</font>' + res.data.msg[i];
			}
			ele += '</div>';
			layer.open({
				type: 0,
				title: '提示',
				content: ele,
				area: ['360px', '260px'],
				btn: ['完善项目', '取消'],
				yes: function(index) {
					window.location.href = '/project/edit/' + unkn.attr('data-id') + '.html#perfect';
					layer.close(index);
				},
				btn2: function(index) {
					layer.close(index);
				}
			});
		}
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 一鍵融資
function submitKeyPush(obj) {
	__getdata_prokeyPush['project_id'] = obj.attr('data-id');
	var ele = '';
	ele += '<img src="' + $('#kp-logo').attr('src') + '" class="pic-circle m-x-auto" />';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">' + $('#kp-title').text() + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">' + $('#kp-words').text() + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">融资轮次：' + obj.attr('data-round') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">所属领域：' + obj.attr('data-industry') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">所在城市：' + obj.attr('data-city') + '</p>';
	ele += '<p class="text-center m-t-2x m-x-1x fs16r">拟融资金额：' + obj.attr('data-money') + '万元</p>';
	layer.open({
		type: 0,
		title: false,
		content: ele,
		area: ['480px', 'auto'],
		btn: ['一键投递', '取消'],
		yes: function(index) {
			layer.close(index);
			layer.load(0, {
				shade: [0.08, '#000']
			});
			// 一鍵投遞
			factory.request('_project_keyPush');
		},
		btn2: function(index) {
			layer.close(index);
		}
	});
}

// 一鍵投遞
function __complete_prokeyPush(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		var str = '';
		str += '本次投递给了' + res.data.count + '位投资人<br />';
		str += '当前项目今日剩余' + res.data.surplus + '投递次数';
		layer.alert(str);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 獲取評估報告列表
function cphistorylists() {
	var _id = $(this).attr('data-id');
	if(!chpdata[_id]) {
		layer.load(0, {
			shade: [0.08, '#000']
		});
		__getdata_procph['project_id'] = _id;
		factory.request('_project_cphistory', _id);
	} else {
		createCph(chpdata[_id]);
	}
}

// 獲取評估報告列表回調
function __complete_procph(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 200) {
		chpdata[unkn] = res.data;
		createCph(res.data);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 顯示歷史評估報告列表
function createCph(data) {
	var ele = '';
	for(var i in data) {
		ele += '<div class="clearfix b-line-b" data-id="' + data[i].id + '" style="display: block; line-height: 72px; padding: 16px;">';
		ele += '<div class="float-left">';
		ele += '<i class="icon-files-pdf fs42r c-f57c00 lhx v-middle"></i>';
		ele += '<font class="fs14r m-l-2x">' + tool.formatDate('yy-mm-dd hh:ii', data[i].create_time) + '</font>';
		ele += '</div>';
		ele += '<a href="' + data[i].cp_uri + '" class="lhx float-right m-l-2x" style="">';
		ele += '<i class="icon-download fs18r"></i>';
		ele += '</a>';
		ele += '<a href="javascript:;" data-uri="' + data[i].cp_uri + '" class="preview lhx float-right m-l-2x">';
		ele += '<i class="icon-to_view fs18r"></i>';
		ele += '</a>';
		ele += '<a href="javascript:;" data-id="' + data[i].id + '" id="btn-delpdf" class="lhx float-right m-l-2x">';
		ele += '<i class="icon-delete fs18r"></i>';
		ele += '</a>';
		ele += '</div>';
	}
	layer.open({
		type: 1,
		title: '评估报告 ',
		content: ele,
		area: ['400px', '480px'],
		btn: ['关闭'],
		yes: function(index) {
			layer.close(index);
		}
	});
}

// showYJRJ
function showYJRJ() {
	layer.open({
		type: 1,
		title: false,
		closeBtn: false,
		area: ['760px', '560px'],
		shade: 0.8,
		id: 'LAY_layuipro',
		resize: false,
		scrollbar: false,
		btn: ['关闭'],
		btnAlign: 'c',
		moveType: 1,
		content: $('#agree-modal-template').html()
	});
}